﻿/**********************************************
 * 功能名称：一些常用的函数类
编写人：  杨莹
建立时间： 杨 2010-12-25
 * 修改时间： 2010-12-28
说明：

 * 
 * 
 **************************************************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace CSM.Controllers
{
    public class PublicMethod
    {

        /// <summary>
        /// 不可逆HASH密码
        /// </summary>
        /// <param name="passwordString"></param>
        /// <returns></returns>
        static public string EncryptPassword(string passwordString)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(passwordString, "MD5");
        }

        /// <summary>
        /// 用于生成一个界于某个区间的自然数。参数为自然数的区间。
        /// </summary>
        /// <param name="passwordString"></param>
        /// <returns></returns>
        static public int CreateId(int MaxNumber, int MinNumber)
        {
            Random MyId = new Random();
            int iResult;
            int iUp = MaxNumber;
            int iDown = MinNumber;
            iResult = MyId.Next(iDown, iUp);
            return iResult;

        }
        /// <summary>
        /// 用于创建ID
        /// </summary>
        /// <returns></returns>
        static public string CreateCustomer()
        {
            string ReturnNumber = "";
            ReturnNumber += "CR";
            ReturnNumber += DateTime.Now.ToString("hhmmss");
            ReturnNumber += CreateId(99999, 10000);
            return ReturnNumber;
        }
        //转出真实的Session
        static public string TranstoTrue(string session)
        {
            string TrueSession = session.Substring(1, 8) + session.Substring(10);
            return TrueSession;

        }
        /// <summary>
        /// 将<p><p/>里面的内容组成一个数组
        /// </summary>
        /// <param name="InputString"></param>
        /// <returns></returns>
        static public List<string> FindNumberParam(string InputString)
        {
            List<string> sResult = new List<string>();

            sResult.Clear();

            if (InputString == "")
            {
                
                sResult.Add("-1");
            }
            int Mylength =  InputString.Length;
            int i= 0;
            int HeadIndex= 0;
            int FootIndex = 0;
            string MySubStr = InputString;
            while (i < InputString.Length)
            {
                //判断变量的括号
                HeadIndex = MySubStr.IndexOf("[");
                FootIndex = MySubStr.IndexOf("]");
                if (HeadIndex == -1 || FootIndex == -1) {  break; }
                if (FootIndex > HeadIndex)
                {
                    sResult.Add(MySubStr.Substring(HeadIndex + 1, FootIndex - HeadIndex - 1).Trim()); 
                   i = FootIndex + 2;
                   MySubStr = MySubStr.Substring(i);
                }
            }



           return sResult;
        }
        /// <summary>
        /// 判断是否是数字
        /// </summary>
        /// <param name="Value">要判断的字符串</param>
        /// <returns></returns>
        static public bool IsNumeric(string Value)
        {
            try
            {
                int i = int.Parse(Value);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 将去除<p>……<p/>部分的题目收集起来
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        static public List<string> ReceiveContent(string Content)
        {
            List<string> Rows = new List<string>();
            Rows.Clear();
            int MyLength = Content.Length;
            int i = 0;
            int Header = 0;
            int Footer = 0;
            string XContent = Content;
            while (i < MyLength)
            {
                Header = XContent.IndexOf("[");
                Footer = XContent.IndexOf("]");
                if (Header == -1 || Footer == -1)
                {
                    Rows.Add(XContent);
                    break;
                }
                i = Footer + 2;
                Rows.Add(XContent.Substring(0, Header));
                XContent = XContent.Substring(i);

            }
            return Rows;


        }
        /// <summary>
        /// 用于从地址中取出省份。
        /// </summary>
        /// <param name="Address"></param>
        /// <returns></returns>
        static public string GetProvince(string Address)
        {
            string Province = Address.Substring(0, 2);
            if (Province == "黑龙")
            {
                Province = "黑龙江";
            }
            if (Province == "内蒙")
            {
                Province = "内蒙古";
            }
            return Province;

        }
        /// <summary>
        /// 将乘以倍数后的金额插入内容中，然后输出
        /// </summary>
        /// <param name="Content"></param>
        /// <param name="Param"></param>
        /// <param name="MoneyTimes"></param>
        /// <param name="YearChange"></param>
        /// <param name="TimeDay"></param>
        /// <returns></returns>
        static public string ReturnMyContent(List<string> Content,List<string> Param, int MoneyTimes,string YearChange,string TimeDay)
        {
            string OutputContent = "";
            for (int i = 0; i < Param.Count(); i++)
            {
                OutputContent += Content[i];
                if (IsNumeric(Param[i]))
                {
                    OutputContent += (Convert.ToInt32(Param[i]) * MoneyTimes).ToString();
                }
                else
                {
                    switch(YearChange)
                    {
                        case "1"://单项的情况
                            {
                                OutputContent += Param[i];
                                break;
                            }
                        default:
                            {
                                if (Param[i].IndexOf("日") != -1 && Param[i].IndexOf("月") != -1 && Param[i].IndexOf("年") != -1)
                                {
                                    int DayIndex = Param[i].IndexOf("日");
                                    int MonthIndex = Param[i].IndexOf("月");
                                    int YearIndex = Param[i].IndexOf("年");
                                    string OldYear = Param[i].Substring(0, YearIndex);//要替换的年份
                                    string OldMonth = Param[i].Substring(YearIndex + 1, MonthIndex - YearIndex);//要替换的月份 eg:12月
                                    string OldDay = Param[i].Substring(MonthIndex + 1, DayIndex - MonthIndex);//要替换的天数  eg:23日

                                    Param[i] = Param[i].Replace(OldYear, YearChange);
                                    Param[i] = Param[i].Replace(OldMonth, "12月");
                                    Param[i] = Param[i].Replace(OldDay, TimeDay + "日");

                                }
                                OutputContent += Param[i];
                                break;
                            }
                    }
                }
            }
            OutputContent += Content[Param.Count()];
            return OutputContent;
        }
        ///// <summary>
        ///// 将模板题库中的题ID（**；**；**；**）的题号集合成一个数组
        ///// </summary>
        ///// <param name="TotalQuestionId"></param>
        ///// <returns></returns>
        //static public List<string> ArrayQuestionId(string TotalQuestionId)
        //{
        //    List<string> Results = new List<string>();
        //    Results.Clear();
        //    int LengthTotal = TotalQuestionId.Length;
        //    int i = 0;
        //    int IndexNum = 0;
        //    string NewTotal = TotalQuestionId ;
        //    while (i < LengthTotal)
        //    {

        //        IndexNum = NewTotal.IndexOf(";");
        //        if (IndexNum == -1)
        //        {
        //            Results.Add(NewTotal);
        //            break;
        //        }
        //        Results.Add(NewTotal.Substring(0, IndexNum));
        //        i = IndexNum + 1;
        //        NewTotal = NewTotal.Substring(i);
        //    }
        //    return Results;

        //}


    }
}
